home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / os2 / pccts.zip / FUNC.G < prev    next >
Text File  |  1992-12-08  |  3KB  |  130 lines

  1. /* F u n c t i o n s */
  2.  
  3. /* recognize a function definition (after the "func_name(...)").
  4.  * If old-style arguments are found, a pointer to the list of
  5.  * arguments is passed in.  If there are no parameters or if new-style
  6.  * definitions are used, $args is NULL.
  7.  */
  8. func_def[AST *args]
  9.         :    <<Sym *p, **save; int oldstyle=0;
  10.               save = zzs_scope(NULL); zzs_scope(&Params);>>
  11.             (    decl[PARAMETER]
  12.                 <<oldstyle=1;>>
  13.             )*
  14.             <<if ($args==NULL && oldstyle)
  15.                 error("we've already got the parameters, thanks");
  16.               else checkArgs( args );
  17.               zzs_scope( save );
  18.             >>
  19.             block
  20.         ;
  21.  
  22. block    :    <<Sym *p, **saveScope=zzs_scope(NULL), *locals=NULL;
  23.               static int level=LOCAL-1;>>
  24.             <<level++; zzs_scope(&locals);>>
  25.  
  26.             "\{"^
  27.                 ( decl[level] )*
  28.                 ( stat[level] )*
  29.             "\}"!
  30.  
  31.             <<--level;
  32.               p = zzs_rmscope(&locals);        /* unlink from sym table */
  33.               pScope(p, "parameters\n");
  34.               zzs_scope(saveScope);            /* return to old scope */
  35.             >>
  36.         ;
  37.  
  38. /*
  39.  * match a statement and yield an expr-tree
  40.  *
  41.  * Label        ":"
  42.  *                  |
  43.  *                  v
  44.  *                WORD
  45.  *
  46.  * expr            expr
  47.  *
  48.  * block        block
  49.  *
  50.  * if            "if"
  51.  *                 |
  52.  *                 v
  53.  *                expr --> stat --> stat        (2nd stat only if else-clause)
  54.  *
  55.  * while        "while"
  56.  *                 |
  57.  *                 v
  58.  *                expr --> stat
  59.  *
  60.  * do            "do"
  61.  *                 |
  62.  *                 v
  63.  *                stat --> expr
  64.  *
  65.  * for            "for"
  66.  *                 |
  67.  *                 v
  68.  *                expr --> expr --> expr -> stat
  69.  *
  70.  * switch        "switch"
  71.  *                 |
  72.  *                 v
  73.  *                expr --> stat
  74.  *
  75.  * case            "case"
  76.  *                 |
  77.  *                 v
  78.  *                expr
  79.  *
  80.  * continue        "continue"
  81.  *
  82.  * break        "break"
  83.  *
  84.  * return        "return"
  85.  *                 |
  86.  *                 v
  87.  *                expr
  88.  *
  89.  * goto            "goto"
  90.  *                 |
  91.  *                 v
  92.  *                WORD
  93.  *
  94.  * ";"
  95.  */
  96. stat[int level]
  97.         :    <<Sym *label;>>
  98.             WORD ":"^
  99.             <<label = zzs_get($1.text);
  100.               if ( label != NULL ) {
  101.                 if ( label->defined )
  102.                     error1("you've already defined label", $1.text);
  103.                 label->defined = 1;
  104.               }
  105.               else {
  106.                   label = addsym(Label, $1.text, $level, NULL, NULL);
  107.                 label->defined = 1;
  108.               }
  109.             >>
  110.         |    expr ";"!
  111.         |    block
  112.         |    "if"^ "\("! expr "\)"! stat[$level] { "else"! stat[$level] }
  113.         |    "while"^ "\("! expr "\)"! stat[$level]
  114.         |    "do"^ stat[$level] "while" "\("! expr "\)"! ";"!
  115.         |    "for"^ "\("! { expr } ";"! { expr } ";"!
  116.             { expr } "\)"!
  117.             stat[$level]
  118.         |    "switch"^ "\("! expr "\)"! stat[$level]
  119.         |    "case"^ expr ":"!
  120.         |    "default"^ ":"!
  121.         |    "continue"^ ";"!
  122.         |    "break" ";"!
  123.         |    "return"^ { expr } ";"!
  124.         |    "goto"^ WORD ";"!
  125.             <<if ( zzs_get($2.text) == NULL )
  126.                   addsym(Label, $2.text, $level, NULL, NULL);
  127.             >>
  128.         |    ";"!
  129.         ;
  130.